package com.zx.map.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zx.map.dao.mapper.ActiveRedpacketMapper;
import com.zx.map.dao.mapper.RedpacketMapper;
import com.zx.map.dao.model.ActiveRedpacket;
import com.zx.map.dao.model.ActiveRedpacketExample;
import com.zx.map.dao.model.Redpacket;
import com.zx.map.dao.model.RedpacketExample;
import com.zx.map.service.ActiveRedpacketService;
import com.zx.map.service.RedpacketService;
@Component("activeRedpacketService")
public class ActiveRedpacketServiceImpl implements ActiveRedpacketService {

	@Autowired
	private RedpacketMapper rmapper;
	
	@Autowired
	private ActiveRedpacketMapper mapper;
	
	@Autowired
	private RedpacketService redpacketService;
	@Override
	public int add(ActiveRedpacket activeRedpacket) {
		return mapper.insertSelective(activeRedpacket);
	}
	@Override
	public void update(ActiveRedpacket activeRedpacket) {
		mapper.updateByPrimaryKeySelective(activeRedpacket);
	}

	@Override
	@Transactional
	public void sendRedpacket(Map<String, Object> condition) throws ParseException {
		
		if(!condition.isEmpty() && condition.containsKey("merchantId")) {
			String merchantId = (String)condition.get("merchantId");
			BigDecimal totalMoney = (BigDecimal)condition.get("totalMoney");
			//查询该商户的满足用户消费金额的红包 
			Redpacket redpacket = redpacketService.queryRedpacketByMerchantId(merchantId, totalMoney);
			
			if(!Objects.isNull(redpacket)) {
				ActiveRedpacket activeRedpacket = new ActiveRedpacket();
				activeRedpacket.setRedpacketId(redpacket.getRedpacketId());
				activeRedpacket.setUserId((String)condition.get("userId"));
				activeRedpacket.setMoney(redpacket.getMoney());
				//目前没有次日生效，只有当日生效
				Date start = redpacket.getBeginDatetime();
				activeRedpacket.setStartDatetime(new Date());
				Date lifeDate = new Date(start.getTime() + 24 * 60 * 60 * 1000 * redpacket.getRedpacketLifecycle());
				activeRedpacket.setEndDatetime(redpacket.getEndDatetime().before(lifeDate) ? redpacket.getEndDatetime() : lifeDate);
				activeRedpacket.setActiveDate(new Date());

				int updateCount  = rmapper.increaseJoinNum(redpacket.getRedpacketId());

				if(updateCount > 0) {
					this.add(activeRedpacket);
				}
			}
		}
		
	}
	@Override
	public Map<String, Object> getActiveRedpacketByUseMoney(String merchantId, String userId, BigDecimal useMoney) {
		Date today = new Date();
		Map<String, Object> result = new HashMap<>();
		RedpacketExample rexample = new RedpacketExample();
		RedpacketExample.Criteria rcriteria = rexample.createCriteria();
		rcriteria.andMerchantIdEqualTo(merchantId);
		rcriteria.andDelStatuEqualTo(false);
		rcriteria.andBeginDatetimeLessThanOrEqualTo(today);
		rcriteria.andEndDatetimeGreaterThanOrEqualTo(today);
		List<Redpacket> repacketList = rmapper.selectByExample(rexample);
		
		if(!repacketList.isEmpty() && Objects.nonNull(repacketList.get(0).getRedpacketId())) {
			ActiveRedpacketExample example = new ActiveRedpacketExample();
			ActiveRedpacketExample.Criteria criteria = example.createCriteria();
			criteria.andRedpacketIdEqualTo(repacketList.get(0).getRedpacketId());
			criteria.andUserIdEqualTo(userId);
			criteria.andIsUsedEqualTo(false);
			example.setOrderByClause(" money desc, end_datetime asc");
			criteria.andStartDatetimeLessThanOrEqualTo(today);
			criteria.andEndDatetimeGreaterThanOrEqualTo(today);
			List<ActiveRedpacket> activeList = mapper.selectByExample(example);
			if(!activeList.isEmpty()) {
				result.put("redpacket", repacketList.get(0));
				result.put("activeRedpacket", activeList.get(0));
			}
		}
		
		return result;
	}
	
}
